﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using SPLists_WS = KALE_Scanning.Sharepoint_Lists_WS;
using SPCopy_WS = KALE_Scanning.Sharepoint_Copy_WS;
using System.Net;
using KALE_Scanning.AGC_WebServices;
using System.ComponentModel;
using System.Collections;

namespace KALE_Scanning
{
    class Methods
    {
        //public static List<System.Windows.Forms.Control> controlsNameList = new List<System.Windows.Forms.Control>();
        public static string Site = "";
        public static string OutgoingRelatedFieldName = "";
        private static readonly String[] Ayirac = new String[] { ";#" };
        public static List<AGC_WebServices.Control> LstMultiLineControl = new List<AGC_WebServices.Control>();
        public static List<AGC_WebServices.LookupFieldValue[]> LstLookupFieldValues = new List<LookupFieldValue[]>();
        public static ErrorProvider errorProvider = new ErrorProvider();


        public static System.Windows.Forms.Control CreateDynamicControl(AGC_WebServices.Control fieldForControls, ref Panel panel, string siteUrl, ref int count, out LookupFieldValue[] _lookupValuesBrim, string libraryName)
        {
            _lookupValuesBrim = null;
            System.Windows.Forms.Control returnControl = null;
            switch (fieldForControls.Type)
            {
                #region Text
                case "Text":
                    Label labelText = CreateLabel(fieldForControls, count);
                    TextBox textBox = CreateTextBox(fieldForControls, count);
                    panel.Controls.Add(labelText);
                    panel.Controls.Add(textBox);
                    returnControl = textBox;

                    if (fieldForControls.IsRequired == true)
                    {
                        //textBox.Validating += new CancelEventHandler(RequiredFieldValidating);
                        textBox.LostFocus += new EventHandler(RequiredFieldValidating);
                    }

                    if (fieldForControls.IsUnique)
                    {
                        textBox.LostFocus += (sender, e) => textBox_LostFocus(sender, e, fieldForControls, siteUrl, libraryName);
                    }
                    count++;
                    break;
                #endregion
                #region Note
                case "Note":
                    Label labelRichText = CreateLabel(fieldForControls, count);
                    RichTextBox richTextBox = CreateRichTextBox(fieldForControls, count);
                    panel.Controls.Add(labelRichText);
                    panel.Controls.Add(richTextBox);
                    returnControl = richTextBox;

                    if (fieldForControls.IsRequired == true)
                    {
                        richTextBox.LostFocus += new EventHandler(RequiredFieldValidating);
                    }

                    count = count + 2;
                    break;
                #endregion
                #region Number
                case "Number":
                    Label labelNumberText = CreateLabel(fieldForControls, count);
                    TextBox numberTextBox = CreateTextBox(fieldForControls, count);
                    numberTextBox.KeyPress += new KeyPressEventHandler(TextBox1KeyPress);
                    panel.Controls.Add(labelNumberText);
                    panel.Controls.Add(numberTextBox);
                    returnControl = numberTextBox;
                    if (fieldForControls.IsRequired == true)
                    {
                        numberTextBox.LostFocus += new EventHandler(RequiredFieldValidating);
                    }

                    if (fieldForControls.IsUnique)
                    {
                        numberTextBox.LostFocus += (sender, e) => textBox_LostFocus(sender, e, fieldForControls, siteUrl, libraryName);
                    }
                    count++;
                    break;
                #endregion
                #region DateTime
                case "DateTime":
                    Label labelDateTime = CreateLabel(fieldForControls, count);
                    DateTimePicker dateTimePicker = CreateDateTimePicker(fieldForControls, count);
                    panel.Controls.Add(labelDateTime);
                    panel.Controls.Add(dateTimePicker);
                    returnControl = dateTimePicker;

                    //if (fieldForControls.IsRequired == true)
                    //{
                    //    dateTimePicker.Validating += new CancelEventHandler(RequiredFieldValidating);
                    //}

                    if (fieldForControls.IsUnique)
                    {
                        dateTimePicker.LostFocus += (sender, e) => dateTimePicker_LostFocus(sender, e, fieldForControls, siteUrl, libraryName);
                    }
                    count++;
                    break;
                #endregion
                #region Choice
                case "Choice":
                    Label labelChoice = CreateLabel(fieldForControls, count);
                    ComboBox comboBoxChoice = CreateComboBox(fieldForControls, count);
                    panel.Controls.Add(labelChoice);
                    panel.Controls.Add(comboBoxChoice);
                    returnControl = comboBoxChoice;
                    LoadComboBox(comboBoxChoice, fieldForControls);
                    Button button = CreateButton(fieldForControls, count);
                    button.Click += (sender, e) => NewChoiceForm(sender, e, fieldForControls, Site);
                    if (fieldForControls.IsRequired == true)
                    {
                        comboBoxChoice.LostFocus += new EventHandler(RequiredFieldValidating);
                    }


                    if (fieldForControls.IsUnique)
                    {
                        comboBoxChoice.LostFocus += (sender, e) => comboBox_LostFocus(sender, e, fieldForControls, siteUrl, libraryName);
                    }
                    panel.Controls.Add(button);
                    count++;
                    break;
                #endregion
                #region Lookup
                case "Lookup":
                    if (fieldForControls.AllowMultipleLines == false)
                    {
                        Label labelLookup = CreateLabel(fieldForControls, count);
                        ComboBox comboBoxLookup = CreateComboBox(fieldForControls, count);
                        Button buttonLookup = CreateButton(fieldForControls, count);
                        buttonLookup.Click += (sender, e) => NewLookupForm(sender, e, fieldForControls, Site, siteUrl);

                        if (fieldForControls.IsRequired == true)
                        {
                            comboBoxLookup.LostFocus += new EventHandler(RequiredFieldValidating);
                        }
                        // Load
                        List<string> choiceLookupFieldsElements = FillChoiceLookupFieldsElements(fieldForControls, siteUrl);
                        List<LookupFieldValue> cleanedChoiceLookupFieldsElements = RemoveSymbols(choiceLookupFieldsElements);
                        LoadChoiceLookupList(comboBoxLookup, cleanedChoiceLookupFieldsElements);
                        comboBoxLookup.DisplayMember = "Value";
                        comboBoxLookup.ValueMember = "LookupID";
                        //if (fieldForControls.IsRequired == true) {
                        //    comboBoxLookup.Validating += new CancelEventHandler(RequiredField_Validating);
                        //}
                        if (fieldForControls.IsUnique)
                        {
                            comboBoxLookup.LostFocus += (sender, e) => comboBox_LostFocus(sender, e, fieldForControls, siteUrl, libraryName);
                        }
                        panel.Controls.Add(labelLookup);
                        panel.Controls.Add(comboBoxLookup);
                        panel.Controls.Add(buttonLookup);
                        returnControl = comboBoxLookup;
                        count++;
                    }
                    else
                    {
                        Label labelCheckLookup = CreateLabel(fieldForControls, count);
                        CheckedListBox checkListBoxLookup = CreateCheckListBox(fieldForControls, count);
                        Button buttonCheckLookup = CreateButton(fieldForControls, count);
                        buttonCheckLookup.Click += (sender, e) => NewLookupForm(sender, e, fieldForControls, Site, siteUrl);

                        if (fieldForControls.IsRequired == true)
                        {
                            checkListBoxLookup.LostFocus += new EventHandler(RequiredFieldValidating);
                        }

                        List<string> choiceLookupFieldsElements = FillChoiceLookupFieldsElements(fieldForControls, siteUrl);
                        List<LookupFieldValue> cleanedChoiceLookupFieldsElements = RemoveSymbols(choiceLookupFieldsElements);
                        _lookupValuesBrim = LoadChoiceLookupList(checkListBoxLookup, cleanedChoiceLookupFieldsElements);
                        panel.Controls.Add(labelCheckLookup);
                        panel.Controls.Add(checkListBoxLookup);
                        panel.Controls.Add(buttonCheckLookup);
                        returnControl = checkListBoxLookup;
                        count = count + 2;
                    }
                    break;
                #endregion
            }
            return returnControl;
        }

        static void comboBox_LostFocus(object sender, EventArgs e, AGC_WebServices.Control fieldForControls, string siteUrl, string libraryName)
        {
            Utility.UniqueDataValidate((sender as System.Windows.Forms.ComboBox), ControlType.Combobox, fieldForControls, siteUrl, libraryName);
        }

        static void dateTimePicker_LostFocus(object sender, EventArgs e, AGC_WebServices.Control fieldForControls, string siteUrl, string libraryName)
        {
            Utility.UniqueDataValidate((sender as System.Windows.Forms.DateTimePicker), ControlType.DateTime, fieldForControls, siteUrl, libraryName);

        }

        static void textBox_LostFocus(object sender, EventArgs e, AGC_WebServices.Control fieldForControls, string siteUrl, string libraryName)
        {
            Utility.UniqueDataValidate((sender as System.Windows.Forms.TextBox), ControlType.Text, fieldForControls, siteUrl, libraryName);

        }

        public static void NewLookupForm(object sender, EventArgs e, AGC_WebServices.Control fieldForControls, string url, string siteUrl)
        {
            List<string> choiceLookupFieldsElements = new List<string>();

            LookupForm lookupForm = new LookupForm(fieldForControls, url, siteUrl);
            lookupForm.ShowDialog();
            if (lookupForm.DialogResult == DialogResult.OK)
            {
                choiceLookupFieldsElements = FillChoiceLookupFieldsElements(fieldForControls, url);
                List<LookupFieldValue> cleanedChoiceLookupFieldsElements = RemoveSymbols(choiceLookupFieldsElements);
                Panel panel = ((Panel)(sender as Button).Parent);
                UpdateLookup(fieldForControls, ref panel, cleanedChoiceLookupFieldsElements);
            }

        }

        public static void NewChoiceForm(object sender, EventArgs e, AGC_WebServices.Control fieldForControls, string url)
        {
            AGC_WebServices.Control control = new AGC_WebServices.Control();
            ChoiceForm choiceForm = new ChoiceForm(fieldForControls, url);
            choiceForm.ShowDialog();
            if (choiceForm.DialogResult == DialogResult.OK)
            {
                using (AGC_WebServices.Service ws = new AGC_WebServices.Service())
                {
                    //ws.UseDefaultCredentials = true;
                    ws.Credentials = UserProperty.UserProperties.Credential;
                    control = ws.GetChoiceField(url, fieldForControls.FieldID);
                    Panel panel = ((Panel)(sender as Button).Parent);
                    UpdateChoice(control, ref panel);
                }
            }
        }

        public static void UpdateChoice(AGC_WebServices.Control control, ref Panel panel)
        {
            foreach (System.Windows.Forms.Control formControl in panel.Controls)
            {
                string staticName = "comboBox" + control.StaticName;
                if (formControl.Name == staticName)
                {
                    ComboBox field = (ComboBox)formControl;
                    field.Items.Clear();
                    foreach (string choices in control.Choices)
                    {
                        field.Items.Add(choices);
                    }
                    field.SelectedItem = Utility.NewChoiceValue;
                    Utility.NewChoiceValue = string.Empty;

                }
            }
        }

        public static void UpdateLookup(AGC_WebServices.Control control, ref Panel panel, List<LookupFieldValue> cleanedChoiceLookupFieldsElements)
        {
            string staticNameComboBox = "comboBox" + control.StaticName;
            string staticNameCheckList = "checkListBox" + control.StaticName;
            foreach (System.Windows.Forms.Control formControl in panel.Controls)
            {
                if (formControl.Name == staticNameComboBox)
                {
                    ComboBox field = (ComboBox)formControl;
                    field.Items.Clear();
                    LoadChoiceLookupList(field, cleanedChoiceLookupFieldsElements);
                    field.DisplayMember = "Value";
                    field.ValueMember = "LookupID";
                    //field.SelectedItem = Utility.NewChoiceValue;
                    //Utility.NewChoiceValue = string.Empty;
                }

                if (formControl.Name == staticNameCheckList)
                {
                    CheckedListBox field = (CheckedListBox)formControl;
                    field.Items.Clear();
                    LoadChoiceLookupList(field, cleanedChoiceLookupFieldsElements);
                    field.DisplayMember = "Value";
                    field.ValueMember = "LookupID";
                }

            }
        }

        public static void LoadComboBox(ComboBox comboBox, AGC_WebServices.Control fieldForControls)
        {
            foreach (string item in fieldForControls.Choices)
            {
                comboBox.Items.Add(item);
            }
        }

        private static CheckedListBox CreateCheckListBox(AGC_WebServices.Control fieldForControls, int count)
        {
            CheckedListBox checkedListBox = new CheckedListBox();
            checkedListBox.FormattingEnabled = true;
            checkedListBox.Location = new System.Drawing.Point(180, (20 + (count * 30)));
            checkedListBox.Name = "checkListBox" + fieldForControls.StaticName;
            checkedListBox.Size = new System.Drawing.Size(180, 50);

            return checkedListBox;
        }

        public static ComboBox CreateComboBox(AGC_WebServices.Control fieldForControls, int count)
        {
            ComboBox comboBox = new ComboBox
                                    {
                                        FormattingEnabled = true,
                                        Location = new System.Drawing.Point(180, (20 + (count * 30))),
                                        Name = "comboBox" + fieldForControls.StaticName,
                                        Size = new System.Drawing.Size(180, 20),
                                        DropDownStyle = ComboBoxStyle.DropDownList
                                    };

            return comboBox;
        }

        public static Button CreateButton(AGC_WebServices.Control fieldForControls, int count)
        {
            Button button = new Button
                                {
                                    Location = new System.Drawing.Point(363, (20 + (count * 30))),
                                    Name = "button" + fieldForControls.StaticName,
                                    Size = new System.Drawing.Size(20, 20),
                                    Text = "+"
                                };

            return button;
        }

        public static Label CreateLabel(AGC_WebServices.Control fieldForControls, int counter)
        {
            Label label = new Label
                              {
                                  AutoSize = true,
                                  Name = "label" + fieldForControls.StaticName,
                                  Text = RequiredLabelControl(fieldForControls),
                                  Location = new System.Drawing.Point(20, (20 + (counter * 30))),
                                  Size = new System.Drawing.Size(20, 30)
                              };

            return label;
        }

        public static TextBox CreateTextBox(AGC_WebServices.Control fieldForControls, int counter)
        {
            TextBox textBox = new TextBox
                                  {
                                      Location = new System.Drawing.Point(180, (20 + (counter * 30))),
                                      Name = "text" + fieldForControls.StaticName,
                                      Size = new System.Drawing.Size(180, 20)
                                  };

            return textBox;

        }

        public static RichTextBox CreateRichTextBox(AGC_WebServices.Control fieldForControls, int counter)
        {
            RichTextBox richTextBox = new RichTextBox
                                          {
                                              Location = new System.Drawing.Point(180, (20 + (counter * 30))),
                                              Name = "richText" + fieldForControls.StaticName,
                                              Size = new System.Drawing.Size(180, 50)
                                          };

            return richTextBox;

            //if (fieldForControls.IsRequired == true) {
            //    richTextBox.Validating += new CancelEventHandler(RequiredField_Validating);
            //}

        }

        public static DateTimePicker CreateDateTimePicker(AGC_WebServices.Control fieldForControls, int counter)
        {
            DateTimePicker dateTimePicker = new DateTimePicker
                                                {
                                                    Location = new System.Drawing.Point(180, (20 + (counter * 30))),
                                                    Name = "dateTime" + fieldForControls.StaticName,
                                                    Size = new System.Drawing.Size(180, 20),
                                                    Format = DateTimePickerFormat.Custom
                                                };

            return dateTimePicker;

        }

        public static string RequiredLabelControl(AGC_WebServices.Control fieldForControls)
        {
            string requiredLabelText;
            if (fieldForControls.IsRequired == true)
            {
                requiredLabelText = fieldForControls.Isim + ": *";
            }
            else
            {
                requiredLabelText = fieldForControls.Isim + ":";
            }
            return requiredLabelText;
        }

        public static List<string> FillChoiceLookupFieldsElements(AGC_WebServices.Control fieldForControls, string sAdres)
        {
            string sourceListId = fieldForControls.LookupListId;
            //outgoingSourceListId = sourceListId;
            //outgoingLookupFieldName = fieldForControls.Isim;
            OutgoingRelatedFieldName = fieldForControls.LookupField;
            List<string> choiceLookupFieldsElements = new List<string>();

            SPLists_WS.Lists forLookUpWs = new SPLists_WS.Lists();
            //forLookUpWs.Credentials = CredentialCache.DefaultCredentials;
            forLookUpWs.Credentials = UserProperty.UserProperties.Credential;
            forLookUpWs.Url = Utility.CheckLastSlash(sAdres) + "_vti_bin/lists.asmx";
            choiceLookupFieldsElements = ReadXmlFieldItems(forLookUpWs.GetListItems(sourceListId, null, null, null, "1000000", null, null).OuterXml, OutgoingRelatedFieldName);

            return choiceLookupFieldsElements;
        }

        public static List<string> ReadXmlFieldItems(string xmlVeri, string fieldName)
        {
            List<string> listeler = new List<string>();

            try
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(xmlVeri);

                foreach (XmlNode xmlAltDugum in xml.ChildNodes)
                {
                    if (xmlAltDugum.Name.ToLower() == "listitems")
                    {
                        foreach (XmlNode xmlListData in xmlAltDugum.ChildNodes)
                        {
                            if (xmlListData.Name.ToLower() == "rs:data")
                            {
                                foreach (XmlNode xmlListEleman in xmlListData.ChildNodes)
                                {
                                    if (xmlListEleman.Name.ToLower() == "z:row")
                                    {
                                        string liste = "";
                                        try
                                        {
                                            if (!String.IsNullOrEmpty(xmlListEleman.Attributes["ows_ID"].Value) && !String.IsNullOrEmpty(xmlListEleman.Attributes["ows_" + fieldName].Value))
                                            {
                                                liste = xmlListEleman.Attributes["ows_ID"].Value + ";#" + xmlListEleman.Attributes["ows_" + fieldName].Value;
                                            }

                                            listeler.Add(liste);
                                        }
                                        catch (Exception ex)
                                        {
                                            // MessageBox.Show(ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return listeler;
            }
            catch (Exception ex)
            {
                listeler = null;
                return listeler;
            }
        }

        public static List<LookupFieldValue> RemoveSymbols(List<string> choiceLookupFieldsElements)
        {
            List<LookupFieldValue> clearedChoiceLookupFieldsElements = new List<LookupFieldValue>();
            for (int i = 0; i < choiceLookupFieldsElements.Count; i++)
            {
                LookupFieldValue val = new LookupFieldValue();
                val.LookupID = Convert.ToInt32(choiceLookupFieldsElements[i].Split(Ayirac, StringSplitOptions.RemoveEmptyEntries)[0]);
                val.Value = choiceLookupFieldsElements[i].Split(Ayirac, StringSplitOptions.RemoveEmptyEntries)[1];
                clearedChoiceLookupFieldsElements.Add(val);
            }

            return clearedChoiceLookupFieldsElements;
        }

        public static LookupFieldValue[] LoadChoiceLookupList(System.Windows.Forms.Control lookupControl, List<LookupFieldValue> lookupFieldValueList)
        {
            if (lookupControl.GetType().ToString() == "System.Windows.Forms.CheckedListBox")
            {
                CheckedListBox chk = (CheckedListBox)lookupControl;
                LookupFieldValue[] diziVal = new LookupFieldValue[lookupFieldValueList.Count];
                for (int i = 0; i < lookupFieldValueList.Count; i++)
                {
                    chk.Items.Add(lookupFieldValueList[i].Value, false);
                    diziVal[i] = lookupFieldValueList[i];
                }
                return diziVal;
            }
            if (lookupControl.GetType().ToString() == "System.Windows.Forms.ComboBox")
            {
                ComboBox cmb = (ComboBox)lookupControl;
                foreach (LookupFieldValue item in lookupFieldValueList)
                {
                    cmb.Items.Add(item);
                }
                return lookupFieldValueList.ToArray();
            }
            return null;
        }

        public static void LoadChoiceLookupList(ComboBox comboBox, List<LookupFieldValue> lookupFieldValueList)
        {
            foreach (LookupFieldValue item in lookupFieldValueList)
            {
                comboBox.Items.Add(item);
            }
        }

        public static List<Sharepoint_Copy_WS.FieldInformation> CreateDynamicCopyField(AGC_WebServices.Control[] fieldClass, ref Hashtable lookupValues, ref  List<System.Windows.Forms.Control> controlsNameList)
        {
            List<Sharepoint_Copy_WS.FieldInformation> abc = new List<Sharepoint_Copy_WS.FieldInformation>();

            int count = 0;
            foreach (AGC_WebServices.Control item in fieldClass)
            {
                if (controlsNameList[count] is TextBox)
                {
                    if (item.Type == "Number")
                    {
                        Sharepoint_Copy_WS.FieldInformation newFieldText = new Sharepoint_Copy_WS.FieldInformation
                        {
                            InternalName = item.StaticName,
                            DisplayName = item.Isim,
                            Type = Sharepoint_Copy_WS.FieldType.Number,
                            Value = controlsNameList[count].Text.Trim() == string.Empty ? "0" : controlsNameList[count].Text.Trim()
                        };
                        abc.Add(newFieldText);
                        count++;
                    }
                    else
                    {
                        Sharepoint_Copy_WS.FieldInformation newFieldText = new Sharepoint_Copy_WS.FieldInformation
                                                                               {
                                                                                   InternalName = item.StaticName,
                                                                                   DisplayName = item.Isim,
                                                                                   Type = Sharepoint_Copy_WS.FieldType.Text,
                                                                                   Value = controlsNameList[count].Text
                                                                               };
                        abc.Add(newFieldText);
                        count++;
                    }
                }
                else if (controlsNameList[count] is ComboBox)
                {
                    if (item.Type == "Lookup")
                    {
                        Sharepoint_Copy_WS.FieldInformation newFieldChoice = new Sharepoint_Copy_WS.FieldInformation
                                                                                 {
                                                                                     InternalName = item.StaticName,
                                                                                     DisplayName = item.Isim,
                                                                                     Type = Sharepoint_Copy_WS.FieldType.Choice,
                                                                                     Value = ""
                                                                                 };
                        ComboBox cmb = controlsNameList[count] as ComboBox;
                        if (cmb.SelectedItem != null && ((LookupFieldValue)cmb.SelectedItem).Value != string.Empty)
                        {
                            LstMultiLineControl.Add(item);

                            AGC_WebServices.LookupFieldValue[] fieldArr = new LookupFieldValue[1];
                            fieldArr[0] = cmb.SelectedItem as LookupFieldValue;

                            LstLookupFieldValues.Add(fieldArr);
                        }
                        else
                        {
                            newFieldChoice.Value = controlsNameList[count].Text;
                        }
                        abc.Add(newFieldChoice);

                        count++;
                    }
                    else if (item.Type == "Choice")
                    {
                        Sharepoint_Copy_WS.FieldInformation newFieldChoice = new Sharepoint_Copy_WS.FieldInformation
                                                                                 {
                                                                                     InternalName = item.StaticName,
                                                                                     DisplayName = item.Isim,
                                                                                     Type = Sharepoint_Copy_WS.FieldType.Choice,
                                                                                     Value = controlsNameList[count].Text
                                                                                 };
                        abc.Add(newFieldChoice);
                        count++;
                    }
                }
                else if (controlsNameList[count] is DateTimePicker)
                {
                    Sharepoint_Copy_WS.FieldInformation newFieldDate = new Sharepoint_Copy_WS.FieldInformation
                                                                           {
                                                                               InternalName = item.StaticName,
                                                                               DisplayName = item.Isim,
                                                                               Type = Sharepoint_Copy_WS.FieldType.DateTime,
                                                                               Value = Convert.ToDateTime(controlsNameList[count].Text).ToString("yyyy-MM-ddTHH:mm:ssZ")
                                                                           };
                    abc.Add(newFieldDate);
                    count++;
                }
                else if (controlsNameList[count] is RichTextBox)
                {
                    Sharepoint_Copy_WS.FieldInformation newFieldNote = new SPCopy_WS.FieldInformation
                                                                           {
                                                                               InternalName = item.StaticName,
                                                                               DisplayName = item.Isim,
                                                                               Type = Sharepoint_Copy_WS.FieldType.Text,
                                                                               Value = controlsNameList[count].Text
                                                                           };
                    abc.Add(newFieldNote);
                    count++;
                }
                else if (controlsNameList[count] is CheckedListBox)
                {

                    CheckedListBox chkList = controlsNameList[count] as CheckedListBox;

                    Sharepoint_Copy_WS.FieldInformation newField = new SPCopy_WS.FieldInformation();
                    newField.InternalName = item.StaticName;
                    newField.DisplayName = item.Isim;
                    newField.Type = Sharepoint_Copy_WS.FieldType.Lookup;
                    if (chkList.CheckedItems.Count > 0)
                    {
                        LstMultiLineControl.Add(item);
                        newField.Value = chkList.CheckedItems[0].ToString();
                        AGC_WebServices.LookupFieldValue[] fieldArr = new LookupFieldValue[chkList.CheckedItems.Count];
                        for (int i = 0; i < chkList.CheckedItems.Count; i++)
                        {
                            //fieldArr[i] = chkList.CheckedItems[i] as LookupFieldValue;
                            LookupFieldValue val = null;
                            LookupFieldValue[] lookupFieldValues = lookupValues[chkList.Name] as LookupFieldValue[];
                            if (lookupFieldValues != null)
                                foreach (LookupFieldValue a in lookupFieldValues)
                                {
                                    if (a.Value == chkList.CheckedItems[i].ToString())
                                    {
                                        val = a;
                                        break;
                                    }
                                }
                            if (val != null)
                            {
                                fieldArr[i] = val;
                            }
                        }
                        LstLookupFieldValues.Add(fieldArr);

                    }
                    else
                    {
                        newField.Value = "";
                    }
                    abc.Add(newField);
                    count++;

                }
                else
                {
                    MessageBox.Show(controlsNameList[count].Name);
                }
            }
            return abc;
        }

        public static AGC_WebServices.Control[] GetFields(string siteAdress, string libraryName, string contentTypeId)
        {
            AGC_WebServices.Control[] controlList;
            using (AGC_WebServices.Service ws = new AGC_WebServices.Service())
            {
                //ws.UseDefaultCredentials = true;

                ws.Credentials = UserProperty.UserProperties.Credential;
                controlList = ws.GetFields(siteAdress, libraryName, contentTypeId);
            }
            return controlList;
        }

        public static void TextBox1KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point    
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        public static void RequiredFieldValidating(object sender, EventArgs e)
        {
            System.Windows.Forms.Control control = sender as System.Windows.Forms.Control;
            if (!Utility.CheckControl(control))
            {
                errorProvider.SetError(control, "Lütfen bu alanı doldurunuz!");
                errorProvider.SetIconAlignment(control, ErrorIconAlignment.BottomLeft);
                errorProvider.BlinkRate = 0;
                control.CausesValidation = false;
                //e.Cancel = true;

                return;
            }
            control.CausesValidation = true;
            errorProvider.SetError(control, "");

        }

    }




}

